using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseForum.Model;
using WebForum.App_Data;
using BaseForum.Dao;

namespace WebForum
{
    public partial class UserInfo : System.Web.UI.Page
    {
        private string userType = "user";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["user"] == null) Response.Redirect("Home.aspx");
                User u = (User)Session["user"];
                TextBoxName.Text = u.Name;
                TextBoxAge.Text = u.Age.ToString();
                TextBoxLocaltion.Text = u.Location;
                TextBoxEmail.Text = u.Email;
                Doctor doctor = DoctorDataSource.Get(u.Id);
                if (doctor != null)
                {
                    userType = "doctor";
                    LabelWorkplace.Visible = true;
                    TextBoxWorkplace.Visible = true;
                    LabelSpecialization.Visible = true;
                    TextBoxSpecialization.Visible = true;
                    LabelExperience.Visible = true;
                    TextBoxExperience.Visible = true;
                    LabelAchievement.Visible = true;
                    TextBoxAchievement.Visible = true;

                    TextBoxWorkplace.Text = doctor.Workplace;
                    TextBoxSpecialization.Text = doctor.Specialization;
                    TextBoxExperience.Text = doctor.Experience;
                    TextBoxAchievement.Text = doctor.Achievement;
                }
            }
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                User user = (User)Session["user"];
                user.Location = TextBoxLocaltion.Text;
                UserDataSource.Update(user);
                if (userType == "doctor")
                {
                    Doctor doctor = DoctorDataSource.Get(user.Id);
                    doctor.Workplace = TextBoxWorkplace.Text;
                    doctor.Specialization = TextBoxSpecialization.Text;
                    doctor.Achievement = TextBoxAchievement.Text;
                    doctor.Experience = TextBoxExperience.Text;
                    DoctorDataSource.Update(doctor);
                }
                Common.Show("Update info success!", "UserInfo.aspx");
            }
            catch (Exception exception)
            {
                Common.Show("Update info faild!" + exception.Message, "UserInfo.aspx");
            }
            Response.Redirect("UserInfo.aspx");
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("AllForum.aspx");
        }

        protected void ButtonChangePass_Click(object sender, EventArgs e)
        {

        }
    }
}
